<?php
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
use App\Models\Apply;
use Illuminate\Http\Request;

class ApplyController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
    //获取用户列表
    public function getApplies(Request $request){
        $applies = Apply::where(function($query) use($request){
                if ($request->input('name') != '') {
                    $query->where('users.nickname', 'like', '%'.$request->input('name').'%');
                }
            })
            ->where(function($query) use($request){
                if ($request->input('phone') != '') {
                    $query->where('users.phone', 'like', '%'.$request->input('phone').'%');
                }
            })
            ->where(function($query) use($request){
                if ($request->input('status_open') != '') {
                    $query->where('users.status',  $request->input('status_open'));
                }
            })
            ->where(function($query) use($request){
                if ($request->input('status_type') == '1') {
                    $query->where('applies.created_at',  '>', date('Y-m-d H:i:s',strtotime('-1 day')));
                }else if ($request->input('status_type') == '2') {
                    $query->where('applies.created_at',  '<=', date('Y-m-d H:i:s',strtotime('-1 day')));
                }
            })
            ->orderby('users.status','asc')
            ->orderby('applies.created_at', 'desc')
            ->join('users', 'users.id', '=' , 'applies.user_id')
            ->select('users.qyh_userid','users.nickname','users.phone','users.total_storage','users.used_storage','users.status','users.synced_with_qyh','applies.created_at','applies.user_id')
            ->paginate(20);
        foreach($applies as $apply){
            if($apply->created_at >  date('Y-m-d H:i:s',strtotime('-1 day'))){
                $apply->status_apply = '未过期';
            }else{
                $apply->status_apply = '已过期';
            }
        }
        $page = isset($page)?$request['page']:1;
        $applies = $applies->appends(array(
            'name'=>$request->input('name'),
            'phone'=>$request->input('phone'),
            'status_open'=>$request->input('status_open'),
            'status_type'=>$request->input('status_type'),
            'page'=>$page
        ));
        return view('admin.list_apply',['applies' => $applies]);
    }
}